﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using fcwms.Common;

//public partial class admin_user_user_edit : System.Web.UI.Page
public partial class admin_user_user_edit : SessionManage
{
    string defaultpassword = "0|0|0|0|0|0|0|0|"; //默认显示密码
    private string action = ActionEnum.Add.ToString(); //操作类型
    private int id = 0;
    protected void Page_Load(object sender, EventArgs e)
    {
        string _action = DTRequest.GetQueryString("action");
        if (!string.IsNullOrEmpty(_action) && _action == ActionEnum.Edit.ToString())
        {
            this.action = ActionEnum.Edit.ToString();//修改类型
            if (!int.TryParse(Request.QueryString["id"] as string, out this.id))
            {
                MessageBox.Jscript(this, "传输参数不正确！", "back", "Error");
                return;
            }
            if (!new fcwms.BLL.dt_users().Exists(this.id))
            {
                MessageBox.Jscript(this, "信息不存在或已被删除！", "back", "Error");
                return;
            }
        }
        if (!IsPostBack)
        {
            //取得用户信息
            fcwms.Model.dt_users model = GetAdminInfo();
            roleBind(model.role_type);
            branchBind();
            if (action == ActionEnum.Edit.ToString())
            {
                ShowInfo(this.id);
            }
        }
    }

    #region 数据绑定==============================
    private void roleBind(int role_type)   //角色绑定
    {
        //StringBuilder strSql = new StringBuilder();
        //strSql.Append("select * from dt_role ");
        //DataTable dt = DbHelperSQL.Query(strSql.ToString()).Tables[0];
        fcwms.BLL.dt_role bll = new fcwms.BLL.dt_role();
        DataTable dt = bll.GetList("").Tables[0];
        ddlRoleId.Items.Clear();
        ddlRoleId.Items.Add(new ListItem("请选择角色...", ""));
        foreach (DataRow dr in dt.Rows)
        {
            if (Convert.ToInt32(dr["role_type"]) >= role_type)
            {
                ddlRoleId.Items.Add(new ListItem(dr["role_name"].ToString(), dr["id"].ToString()));
            }
        }
        //    DataSet ds = DbHelperSQL.Query(strSql.ToString());
        //    this.ddlRoleId.DataSource = ds.Tables[0];
        //    this.ddlRoleId.DataTextField = "role_name";//设置列表显示的字段名称
        //    this.ddlRoleId.DataValueField = "role_name";//设置列表显示的字段名称对应的值,一般唯一的主键
        //    this.ddlRoleId.DataBind();
        //    this.ddlRoleId.Items.Insert(0, new ListItem("请选择角色...", ""));//第一项中加入内容,重点是绑定后添加 请选择角色...
    }

    private void branchBind()                     //部门绑定
    {
        StringBuilder strSql = new StringBuilder();
        strSql.Append("select * from dt_branch ");
        DataSet ds = DbHelperSQL.Query(strSql.ToString());
        this.ddlBranch.DataSource = ds.Tables[0];
        this.ddlBranch.DataTextField = "branch_name";//设置列表显示的字段名称
        this.ddlBranch.DataValueField = "id";//设置列表显示的字段名称对应的值，一般唯一的主键
        this.ddlBranch.DataBind();
        this.ddlBranch.Items.Insert(0, new ListItem("请选择部门...", ""));//第一项中加入内容,重点是绑定后添加 请选择角色...
    }
    #endregion

    #region 赋值操作==============================
    public void ShowInfo(int _id)
    {
        fcwms.BLL.dt_users bll = new fcwms.BLL.dt_users();
        fcwms.Model.dt_users model = bll.GetModel(_id);

        ddlRoleId.Value = model.role_id.ToString();
        //ddlBranch.SelectedValue = model.branch_id.ToString();
        ddlBranch.Value = model.branch_id.ToString();
        txtUserID.Text = model.user_id;
        txtUserID.ReadOnly = true;
        txtUserID.Enabled = false;
        txtUserName.Text = model.user_name;
        if (!string.IsNullOrEmpty(model.user_pwd))
        {
            txtUserPwd.Attributes["value"] = txtUserPwd1.Attributes["value"] = defaultpassword;
        }
        //txtUserPwd.Attributes["value"] = txtUserPwd1.Attributes["value"] = model.user_pwd;
        txtTelephone.Text = model.telephone;
        txtEmail.Text = model.email;
        rblIsLock.SelectedValue = model.is_lock.ToString();
    }
    #endregion

    #region 增加操作==============================
    private bool DoAdd()
    {
        bool result = true;
        fcwms.BLL.dt_users bll = new fcwms.BLL.dt_users();
        fcwms.Model.dt_users model = new fcwms.Model.dt_users();

        model.user_id = txtUserID.Text.Trim();
        model.user_pwd = DESEncrypt.Encrypt(txtUserPwd.Text.Trim());
        model.role_id = int.Parse(ddlRoleId.Value);
        model.role_type = new fcwms.BLL.dt_role().GetModel(model.role_id).role_type;
        model.user_name = txtUserName.Text.Trim();
        model.branch_id = int.Parse(ddlBranch.Value);
        model.telephone = txtTelephone.Text.Trim();
        model.email = txtEmail.Text.Trim();
        model.is_lock = int.Parse(rblIsLock.SelectedValue);
        //model.add_time = DateTime.Now;
        //if (!bll.Exists(model.user_id))
        //{
        if (bll.Add(model) < 1)
        {
            result = false;
        }
        //}
        //else
        //{
        //    MessageBox.Jscript(this, "此用户已存在，请换一个", "", "Warning");
        //}
        return result;
    }
    #endregion

    #region 修改操作==============================
    private bool DoEdit(int _id)
    {
        bool result = true;
        fcwms.BLL.dt_users bll = new fcwms.BLL.dt_users();
        fcwms.Model.dt_users model = bll.GetModel(_id);
        model.role_id = int.Parse(ddlRoleId.Value);
        model.role_type = new fcwms.BLL.dt_role().GetModel(model.role_id).role_type;
        //判断密码是否更改
        if (txtUserPwd.Text.Trim() != defaultpassword)
        {
            model.user_pwd = DESEncrypt.Encrypt(txtUserPwd.Text.Trim());
        }
        //if (txtUserPwd.Text.Trim() != model.user_pwd)
        //    model.user_pwd = DESEncrypt.Encrypt(txtUserPwd.Text.Trim());
        model.user_name = txtUserName.Text.Trim();
        model.branch_id = int.Parse(ddlBranch.Value);
        model.telephone = txtTelephone.Text.Trim();
        model.email = txtEmail.Text.Trim();
        model.is_lock = int.Parse(rblIsLock.SelectedValue);
        if (!bll.Update(model))
        {
            result = false;
        }
        return result;
    }
    #endregion

    #region 保存操作==============================
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        if (action == ActionEnum.Edit.ToString()) //修改
        {
            ChkAdminLevel("sys_user", ActionEnum.Edit.ToString()); //检查权限
            if (!DoEdit(this.id))
            {
                MessageBox.Jscript(this, "保存过程中发生错误啦！", "", "Error");
                return;
            }
            MessageBox.Jscript(this, "修改用户成功啦", "user_list.aspx", "Success");
        }
        else //添加
        {
            ChkAdminLevel("sys_user", ActionEnum.Add.ToString()); //检查权限
            if (!DoAdd())
            {
                MessageBox.Jscript(this, "保存过程中发生错误啦！", "", "Error");
                return;
            }
            MessageBox.Jscript(this, "添加用户成功啦", "user_list.aspx", "Success");
        }
    }
    #endregion
}